package com.sam.enumeration;

import java.util.EnumMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * 香草类
 * 
 * @author Administrator
 * 
 */
public class Herb {

	public enum Type {
		ANNUAL, PERENNIAL, BIENNIAL
	}

	private final String name;

	private final Type type;

	public Herb(String name, Type type) {

		this.name = name;
		this.type = type;
	}

	public String getName() {
		return name;
	}

	public Type getType() {
		return type;
	}

	@Override
	public String toString() {

		return name;
	}

	public static void main(String[] args) {

		/** 花园 **/
		Herb garden[] = new Herb[8];

		/** 创建植物 **/
		garden[0] = new Herb("香草1", Type.ANNUAL);
		garden[1] = new Herb("香草2", Type.PERENNIAL);
		garden[2] = new Herb("香草3", Type.ANNUAL);
		garden[3] = new Herb("香草4", Type.BIENNIAL);
		garden[4] = new Herb("香草5", Type.PERENNIAL);
		garden[5] = new Herb("香草6", Type.BIENNIAL);
		garden[6] = new Herb("香草7", Type.PERENNIAL);
		garden[7] = new Herb("香草8", Type.BIENNIAL);

		/** 创建按类型分类的草本集合 **/
		@SuppressWarnings("unchecked")
		Set<Herb>[] herbByType = (Set<Herb>[]) new Set[Herb.Type.values().length];

		for (int i = 0; i < herbByType.length; i++) {

			herbByType[i] = new HashSet<Herb>();

		}
		/** 利用枚举original分类 **/
		for (Herb herb : garden) {

			herbByType[herb.getType().ordinal()].add(herb);

		}

		/** 打印 **/
		for (int j = 0; j < herbByType.length; j++) {

			System.out.println(Herb.Type.values()[j] + ":" + herbByType[j]);

		}

		/** 更好的解决方案EnumMap **/
		Map<Herb.Type, Set<Herb>> herbByType2 = new EnumMap<Herb.Type, Set<Herb>>(
				Herb.Type.class);

		for (Herb.Type type2 : Herb.Type.values()) {

			herbByType2.put(type2, new HashSet<Herb>());

		}

		/** 分类存储 **/
		for (Herb herb2 : garden) {

			herbByType2.get(herb2.getType()).add(herb2);

		}

		System.out.println(herbByType2);

	}

}
